package com.ldk.base.framework.util.sms;

import com.alibaba.fastjson.JSON;
import com.ldk.base.framework.util.EncryptUtil;
import com.ldk.base.framework.util.HttpUtil;
import com.ldk.base.framework.util.SystemUtil;

import org.apache.commons.lang.StringUtils;

import java.net.URLEncoder;

/**
 * 互亿无线短信API
 * @author mqy
 *
 */
public class HuyiSmsApi implements ISmsApi {

	private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
	//private static String Url = "http://106.ihuyi.com/webservice/sms.php?method=Submit";

	@Override
	public Boolean send(SmsConfig config) {
		if(config == null) return false;
		if(StringUtils.isBlank(config.getAccessKey()) ||
				StringUtils.isBlank(config.getAccessPwd()) ||
				StringUtils.isBlank(config.getPhonNos()) ||
				StringUtils.isBlank(config.getContent()))
			return false;
		String encodeContent = config.getContent();
		try{
			encodeContent = URLEncoder.encode(config.getContent(), "utf-8");
		}
		catch(Exception e){

		}
		String params=String.format("account=%s&password=%s&mobile=%s&content=%s&format=json",
				config.getAccessKey(),
				config.getAccessPwd(),
				config.getPhonNos(),
				encodeContent);

		String result = HttpUtil.get(Url, params, null);
		//System.out.println("huyiSms::Response::"+result);
		if(StringUtils.isEmpty(result))
			return false;
		HuyiSmsResult huyiSmsResult = JSON.parseObject(result, HuyiSmsResult.class);
		if(huyiSmsResult != null && huyiSmsResult.getCode().equalsIgnoreCase("2")){
			return true;
		}
		return false;
	}

	/**
	 * 互亿无线响应对象
	 * @author mqy
	 *
	 */
	public static class HuyiSmsResult{
		private  String code;
		private  String msg;
		private  String smsid;

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public String getMsg() {
			return msg;
		}

		public void setMsg(String msg) {
			this.msg = msg;
		}

		public String getSmsid() {
			return smsid;
		}

		public void setSmsid(String smsid) {
			this.smsid = smsid;
		}
	}
}
